<style lang="less"></style>
<template>
  <div class>
    <div class="table-basic-vue frame-page h-panel">
      <div class="h-panel-bar">
        <span class="h-panel-title">添加视频</span>
      </div>
      <div class="h-panel-body">
        <Form
          ref="form"
          mode="block"
          :validOnChange="true"
          :showErrorTip="true"
          :labelWidth="110"
          :rules="rules"
          :model="video"
        >
          <FormItem label="所属课程" prop="course_id">
            <template v-slot:label>所属课程</template>
            <Select
              v-model="video.course_id"
              :datas="courses"
              keyName="id"
              titleName="title"
              :filterable="true"
              @change="selectCourse"
            ></Select>
          </FormItem>
          <FormItem label="章节" prop="chapter_id">
            <template v-slot:label>章节</template>
            <Select
              v-model="video.chapter_id"
              :datas="chapters"
              keyName="id"
              titleName="title"
              :filterable="true"
            ></Select>
          </FormItem>
          <FormItem label="视频名" prop="title">
            <template v-slot:label>视频名</template>
            <input type="text" v-model="video.title" />
          </FormItem>
          <FormItem label="价格" prop="charge">
            <template v-slot:label>价格</template>
            <div class="h-input-group" v-width="200">
              <input type="text" v-model="video.charge" />
              <span class="h-input-addon">元</span>
            </div>
          </FormItem>
          <FormItem label="禁止单独购买" prop="is_ban_sell">
            <template v-slot:label>禁止单独购买</template>
            <h-switch v-model="video.is_ban_sell" :trueValue="1" :falseValue="0"></h-switch>
          </FormItem>
          <FormItem label="简短介绍" prop="short_description">
            <template v-slot:label>简短介绍</template>
            <textarea v-model="video.short_description"></textarea>
          </FormItem>
          <FormItem label="详细介绍" prop="description">
            <template v-slot:label>详细介绍</template>
            <tinymce-editor v-model="video.original_desc"></tinymce-editor>
          </FormItem>
          <FormItem label="上架时间" prop="published_at">
            <template v-slot:label>上架时间</template>
            <DatePicker v-model="video.published_at" v-width="200" type="datetime"></DatePicker>
          </FormItem>
          <FormItem label="是否显示" prop="is_show">
            <template v-slot:label>是否显示</template>
            <h-switch v-model="video.is_show" :trueValue="1" :falseValue="0"></h-switch>
          </FormItem>

          <FormItem label="上传视频">
            <template v-slot:label>上传视频</template>
            <Tabs :datas="tabs" v-model="tab"></Tabs>
          </FormItem>

          <FormItem label="阿里云视频ID" prop="aliyun_video_id" v-show="tab === '阿里云'">
            <template v-slot:label>阿里云视频ID</template>
            <aliyun-video v-model="video.aliyun_video_id"></aliyun-video>
          </FormItem>
          <FormItem label="腾讯云视频ID" prop="tencent_video_id" v-show="tab === '腾讯云'">
            <template v-slot:label>腾讯云视频ID</template>
            <tencent-video v-model="video.tencent_video_id"></tencent-video>
          </FormItem>
          <FormItem label="视频URL地址（以mp4,m3u8等格式结尾的链接）" prop="url" v-show="tab === '直链'">
            <template v-slot:label>视频URL地址（以mp4,m3u8等格式结尾的链接）</template>
            <input type="text" v-model="video.url" />
          </FormItem>

          <FormItem label="视频时长" prop="duration">
            <template v-slot:label>视频时长</template>
            <div class="h-input-group" v-width="200">
              <input type="text" v-model="video.duration" />
              <span class="h-input-addon">秒</span>
            </div>
          </FormItem>

          <FormItem label="试看时长" prop="free_seconds">
            <template v-slot:label>试看时长</template>
            <div class="h-input-group" v-width="200">
              <input type="text" v-model="video.free_seconds" />
              <span class="h-input-addon">秒</span>
            </div>
          </FormItem>

          <FormItem label="禁止快进" prop="ban_drag">
            <template v-slot:label>禁止快进</template>
            <h-switch v-model="video.ban_drag" :trueValue="1" :falseValue="0"></h-switch>
          </FormItem>

          <FormItem label="评论开关" prop="comment_status">
            <template v-slot:label>评论开关</template>
            <Select v-model="video.comment_status" :datas="commentStatus"></Select>
          </FormItem>

          <FormItem label="Web播放器" prop="player_pc">
            <template v-slot:label>Web播放器</template>
            <Select v-model="video.player_pc" :datas="playerPc"></Select>
          </FormItem>

          <FormItem label="手机播放器" prop="player_h5">
            <template v-slot:label>手机播放器</template>
            <Select v-model="video.player_h5" :datas="playerH5"></Select>
          </FormItem>

          <FormItem label="SEO描述" prop="seo_description">
            <template v-slot:label>SEO描述</template>
            <textarea v-model="video.seo_description"></textarea>
          </FormItem>
          <FormItem label="SEO关键字" prop="seo_keywords">
            <template v-slot:label>SEO关键字</template>
            <textarea v-model="video.seo_keywords"></textarea>
          </FormItem>

          <FormItem>
            <Button color="primary" @click="create">添加</Button>
          </FormItem>
        </Form>
      </div>
    </div>
  </div>
</template>
<script>
import TinymceEditor from '../common/tinymce';
import AliyunVideo from '../common/video/aliyun/aliyun';
import TencentVideo from '../common/video/tencent/tencent';

import Video from 'model/Video';

export default {
  components: {
    TinymceEditor,
    AliyunVideo,
    TencentVideo
  },
  data() {
    return {
      video: Video.parse({}),
      courses: [],
      chapters: [],
      tabs: ['阿里云', '腾讯云', '直链'],
      tab: '阿里云',
      commentStatus: [
        {
          title: '禁止评论',
          key: 0
        },
        {
          title: '所有人',
          key: 1
        },
        {
          title: '仅订阅',
          key: 2
        }
      ],
      playerPc: [
        {
          title: '默认',
          key: 'xg'
        },
        {
          title: '腾讯云',
          key: 'tencent'
        },
        {
          title: '阿里云',
          key: 'aliyun'
        }
      ],
      playerH5: [
        {
          title: '默认',
          key: 'xg'
        },
        {
          title: '腾讯云',
          key: 'tencent'
        },
        {
          title: '阿里云',
          key: 'aliyun'
        }
      ],
      rules: {
        required: [
          'course_id',
          'title',
          'charge',
          'short_description',
          'published_at',
          'is_show',
          'is_ban_sell',
          'ban_drag',
          'player_pc',
          'duration',
          'player_h5',
          'free_seconds',
          'comment_status'
        ]
      }
    };
  },
  mounted() {
    this.init();
  },
  methods: {
    init() {
      this.video.is_show = 0;
      this.video.is_ban_sell = 0;
      this.video.ban_drag = 0;
      this.video.comment_status = 0;
      this.video.player_pc = 'xg';
      this.video.player_h5 = 'xg';

      // 读取创建所需要的参数
      R.Video.Create().then(resp => {
        this.courses = resp.data.courses;
      });
    },
    back() {
      this.$router.push({ name: 'Video' });
    },
    create() {
      let validResult = this.$refs.form.valid();
      if (validResult.result) {
        this.video.render_desc = this.video.original_desc;
        this.$emit('success', this.video);
      }
    },
    selectCourse(course) {
      R.CourseChapter.List({ course_id: course.id }).then(resp => {
        this.chapters = resp.data.chapters;
      });
    }
  }
};
</script>
